<?php

session_start();
$type = @$_REQUEST['type'];

//===== ### 请求token

if($_REQUEST['type'] == 'token'){
	echo $token = md5(time()).md5(rand());
	$_SESSION[$_REQUEST['token_name']]= $token;
	exit;
}

//===== ### 验证token

$token_arr = array(		// 跳过token验证的type项 添加到这里

);
if(!in_array($_REQUEST['type'],$token_arr)){
	if(!@$_REQUEST['token'] || $_REQUEST['token'] != $_SESSION[$_REQUEST['type']]){
		$_REQUEST['token'] = md5(rand());
		exit('10001');
	}
	$_REQUEST['token'] = md5(rand());
}


require_once 'Include/config.php';
require_once 'Include/function.php';

//===== 公共变量
$ServiceListPath = 'Include/service_list';		//服务列表存储文件


switch($type){

	//登陆
	case 'login':
		$user = @$_REQUEST['user'];
		$pwd = @$_REQUEST['pwd'];
		if(!$user || !$pwd){
			exit('err');
		}
		if($user != 'admin' || $pwd != '123'){
			exit('err2');
		}
		exit('ok');
		break;



	//多选删除
	case 'DeleteMulti':
		$paths = @$_REQUEST['paths'];
		$service = @$_REQUEST['service'];
		$cache_path = @$_REQUEST['cache_path'];
		if(!$paths || !$service || !$cache_path){
			exit('err');
		}

		$path_arr = explode(',',$paths);
		if(count($path_arr) > 0){
			$config['upyun_bucketname'] = $service;
			$res = UpyunDelete($config,$path_arr,1);
			if(file_exists($cache_path)){
				unlink($cache_path);
			}
		}
		exit('ok');
		break;

	//刷新
	case 'refresh':
		$path = $_REQUEST['path'];
		$refresh_type = $_REQUEST['refresh_type'];
		
		if($refresh_type == 'refresh'){
			if(file_exists($path)){
				unlink($path);
			}
		}else{
			rmdirs('Cache');			
		}
		break;




	//文件上传
	case 'upload_img':
		$path = @$_REQUEST['path'];
		$cache_path = @$_REQUEST['cache_path'];
		$service = @$_REQUEST['service'];
		$FileName = @$_FILES['file']['name'];

		if(!$path || !$cache_path || !$service || !$FileName){
			exit('err');
		}

		if($path == '/'){
			$path = '/'.$FileName;
		}else{
			$path = $path.'/'.$FileName;
		}

		$config['upyun_bucketname'] = $service;
		$res = UpyunAddFile($config,$path,file_get_contents($_FILES['file']['tmp_name']));
		if($res && is_array($res)){
			if(file_exists($cache_path)){
				unlink($cache_path);
			}
			exit('ok');
		}else{
			exit('err1');
		}
		break;



	//创建目录
	case 'CreateDir':
		$path = $_REQUEST['path'];
		$cache_path = $_REQUEST['cache_path'];
		$service = $_REQUEST['service'];
		if(!$path || !$cache_path || !$service){
			exit('err1');
		}
		$config['upyun_bucketname'] = $service;
		$res = UpyunCreateDir($config,$path);
		if($res){
			if(file_exists($cache_path)){
				unlink($cache_path);
			}
			exit('ok');
		}else{
			exit('err2');
		}
		break;




	//删除指定文件
	case 'DelFileDir':
		$path = $_REQUEST['path'];
		$service = $_REQUEST['service'];
		$cache_path = $_REQUEST['cache_path'];
		if(!$path || !$service){
			exit('err');
		}
		$config['upyun_bucketname'] = $service;
		$res = UpyunDelete($config,$path);
		if($res){
			if(file_exists($cache_path)){	//删除当前目录内容的缓存文件
				unlink($cache_path);
			}
			exit('ok');
		}else{
			exit('err1');
		}
		break;	




	//获取指定目录内容
	case 'GetDirContent':
		$path = $_REQUEST['path'];
		$service = $_REQUEST['service'];
		require_once('Include/UpYun/vendor/autoload.php');
		$config['upyun_bucketname'] = $service;
		$serviceConfig = new \Upyun\Config($config['upyun_bucketname'],$config['upyun_username'],$config['upyun_password']);
		$up = new \Upyun\Upyun($serviceConfig);
		$res = $up->read($path,null,array('X-List-Order'=>'desc'));
		
		$res2 = json_encode($res);
		$path2 = 'Cache/'.md5($service.$path).'.html';
		mkdirs(dirname($path2));
		file_put_contents($path2,$res2);
		echo $res2;
		break;




	//获取服务列表
	case 'GetServiceList':
		if(!file_exists($ServiceListPath)){
			exit('err1');
		}
		echo file_get_contents($ServiceListPath);
		break;



	//添加服务
	case 'add_service':
		$service_name = $_REQUEST['service_name'];
		$service_url = $_REQUEST['service_url'];
		if(!$service_name || !$service_url){
			exit('err1');	//缺少参数
		}
		
		if(!file_exists($ServiceListPath)){		//无则添加
			$arr = array('name'=>$service_name,'url'=>$service_url);
			$res = json_encode(array($arr));
			file_put_contents($ServiceListPath,$res);
		}else{						//有则追加
			$res = file_get_contents($ServiceListPath);
			if(!preg_match('/^\[{.*}\]$/',$res)){
				unlink($ServiceListPath);
				exit('err3');
			}
			$res2 = json_decode($res,1);
			foreach($res2 as $v){
				if($v['name'] == $service_name){
					exit('err2');	//服务已存在
				}
			}
			$arr = array('name'=>$service_name,'url'=>$service_url);
			$res2[] = $arr;
			$res3 = json_encode($res2);
			file_put_contents($ServiceListPath,$res3);
		}
		exit('ok');
	break;


	//删除服务
	case 'del_service':
		$name = $_REQUEST['name'];
		if(!$name){
			exit('err1');
		}
		if(!file_exists($ServiceListPath)){
			exit('err2');
		}
		$res = file_get_contents($ServiceListPath);
		if(!preg_match('/^\[{.*}\]$/',$res)){
			unlink($ServiceListPath);
			exit('err3');
		}
		$res2 = json_decode($res,1);
		$arr = [];
		foreach($res2 as $v){
			if($v['name'] != $name){
				$arr[] = $v;
			}
		}
		if(count($arr) > 0){
			file_put_contents($ServiceListPath,json_encode($arr));
		}else{
			unlink($ServiceListPath);
		}
		echo 'ok';
		break;




	default :
		echo 'default';
	break;


}

